package com.example.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.exception.CustomException;
import com.example.mapper.DeviceMapper;
import com.example.pojo.entity.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.service.IDeviceService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-07-16
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements IDeviceService {

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public Page<Device> selectPage(Device device, Integer pageNum, Integer pageSize) {
        MPJLambdaWrapper<Device> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper
                .selectAll(Device.class)
                .selectAs(Admin::getName,Device::getResponsiblePersonName)
                .selectAs(Team::getName,Device::getTeamName)
                .innerJoin(Admin.class,Admin::getId,Device::getResponsiblePersonId)
                .innerJoin(Team.class,Team::getId,Device::getTeamId)
                .eq(Device::getIsDeleted,0)
                .eq(ObjectUtil.isNotNull(device.getType()),Device::getType,device.getType())
                .eq(ObjectUtil.isNotNull(device.getTeamId()),Device::getTeamId,device.getTeamId());

        return page(new Page<>(pageNum, pageSize), mpjLambdaWrapper);
    }

    @Override
    public void add(Device device) {
        device.setIsDeleted(0);
        device.setIsDeleted(0);
        deviceMapper.insert(device);
    }

    @Override
    public void deleteById(Integer id) {
        Device device = deviceMapper.selectById(id);
        device.setIsDeleted(1);
        updateById(device);
    }



}
